<?php
    class TokenDAL{
        //插入新的token
        public function insertToken($uid,$token){
            $result=false;
            $query="insert into tb_token_mobile(uid,token,modification_time) values(:uid,:token,now());";
            $pdo=myPDO::getInstance();
            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();

                $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
                $statement->bindValue(':uid',$uid);
                $statement->bindValue(':token',$token);
                $result=$statement->execute();

                $pdo->commit();
            }catch(Exception $e){
                $pdo->rollBack();
                //echo "Failed: ".$e->getMessage();
            }
            $statement->closeCursor();
            
            return $result;
        }
        //更新token
        public function updateToken($uid,$token){
            $query="update tb_token_mobile set token=:token,modification_time=now() where uid=:uid;";

            $pdo=myPDO::getInstance();
            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();

                $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
                $statement->bindValue(':uid',$uid);
                $statement->bindValue(':token',$token);
                $result=$statement->execute();

                $pdo->commit();
            }catch(Exception $e){
                $pdo->rollBack();
                //echo "Failed: ".$e->getMessage();
            }
            $statement->closeCursor();

            return $result;
        }
        //验证用户是否已存在token
        public function isUserToken($uid){
            $query="select count(1) as count from tb_token_mobile where uid=:uid;";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
            $statement->bindValue(':uid',$uid);
            if($statement->execute()){
                $result=$statement->fetch(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();

            return $result;
        }
        //判断token是否已存在
        public function isToken($token){
            $query="select count(1) as count from tb_token_mobile where token=:token;";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
            $statement->bindValue(':token',$token);
            if($statement->execute()){
                $result=$statement->fetch(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();

            return $result;
        }
        //获取token信息
        public function getTokenInfo($token){
            $query="select tb_account.uid,token,level,modification_time 
            from tb_token_mobile,tb_account 
            where tb_account.uid=tb_token_mobile.uid 
            and tb_account.status=1 
            and tb_token_mobile.token=:token;";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
            $statement->bindValue(':token',$token);
            if($statement->execute()){
                $result=$statement->fetch(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();

            return $result;
        }

        public function deleteToken(){

        }
    }
?>